package modelimplementation

import (
	"math"
	"salarypayment-case/Model"
	"time"
)

type HourlyClassification struct {
	itsRate      float64
	itsTimeCards map[time.Time]TimeCard
}

func CreateHourlyClassification(hourlyRate float64) *HourlyClassification {
	return &HourlyClassification{itsRate: hourlyRate}
}

func (hc *HourlyClassification) GetRate() float64 {
	return hc.itsRate
}

func (hc *HourlyClassification) GetTimeCard(date time.Time) TimeCard {
	return hc.itsTimeCards[date]
}

func (hc *HourlyClassification) AddTimeCard(tc TimeCard) {
	hc.itsTimeCards[tc.GetDate()] = tc
}

func (hc *HourlyClassification) CalculatePayForTimeCard(tc TimeCard) float64 {
	hours := tc.GetHours()
	overtime := math.Max(0.0, hours-8.0)
	straightTime := hours - overtime
	return straightTime * hc.itsRate + overtime * hc.itsRate * 1.5
}

func (hc *HourlyClassification) CalculatePay(pc Model.Paycheck) float64 {
	totalPay := 0.0
	
	for date , tc := range hc.itsTimeCards {
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()
		if date.After(dt1) && date.Before(dt2) {
			totalPay += hc.CalculatePayForTimeCard(tc)
		}
	}

	return totalPay
}
